﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client.Events;
using System.Text;

namespace DiffuseEnjoyIdle.MessageCenter.RabbitMQ.Handler
{
    public class TestHandler : IMQHandler
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly ILogger<TestHandler> _logger;

        public TestHandler(IServiceProvider serviceProvider, ILogger<TestHandler> logger)
        {
            _serviceProvider = serviceProvider;
            _logger = logger;
        }

        public string QueueName => RabbitMQKeyName.TextQueue;

        public async Task ReceivedHandler(object obj, BasicDeliverEventArgs args)
        {
            try
            {
                var message = Encoding.UTF8.GetString(args.Body.ToArray());
                using var scope = _serviceProvider.CreateScope();


                ((EventingBasicConsumer)obj).Model.BasicAck(args.DeliveryTag, false);
                _logger.LogInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "测试通过");
            }
            catch (Exception ex)
            {

                throw ex;
            }



        }
    }
}
